package com.kjsdfgk.hjmdsf.charge.mvp.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.TimeUtils;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import com.kjsdfgk.hjmdsf.app.entity.Account;
import com.kjsdfgk.hjmdsf.app.utils.SharedPrefsUtils;
import com.kjsdfgk.hjmdsf.charge.di.component.DaggerMemberChargeComponent;
import com.kjsdfgk.hjmdsf.charge.mvp.contract.MemberChargeContract;
import com.kjsdfgk.hjmdsf.charge.mvp.presenter.MemberChargePresenter;
import com.lksdkjs.live.R;


import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 04/17/2020 17:46
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class MemberChargeActivity extends BaseActivity<MemberChargePresenter> implements MemberChargeContract.View {

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerMemberChargeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_member_charge; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }
    @BindView(R.id.tv_user_name)
    TextView tv_user_name;
    @BindView(R.id.tv_user_expire_time)
    TextView expire_time;
    @BindView(R.id.edit_kami)
    EditText edit_kami;
    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        Account account = mPresenter.getAccount();

        if (account!=null){
            tv_user_name.setText("用    户："+account.getName());
            expire_time.setText("到    期："+ TimeUtils.millis2String(Long.parseLong(account.getVip())*1000) );
        }
    }

    @Override
    public void showLoading(String key) {
        loadingDialog.show(new Date().toString());
    }

    @Override
    public void hideLoading() {
        loadingDialog.dismiss();
    }

    @Override
    public boolean isCancel() {
        return false;
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @OnClick({R.id.btn_paste_kami, R.id.btn_use_kami, R.id.btn_buy_kami })
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.btn_paste_kami:
                edit_kami.setText("8DZDPeD1xK");
                edit_kami.setSelection(edit_kami.getText().toString().length());
                break;
            case R.id.btn_use_kami:
                if (edit_kami.getText().toString().length() == 10){
                    mPresenter.cardCharge(edit_kami.getText().toString());
                }else {
                    showMessage("卡密错误 ，请检查");
                }

                break;
            case R.id.btn_buy_kami:

                String url = SharedPrefsUtils.getString("kamiUrl","");
                if (!TextUtils.isEmpty(url)){
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                }else {
                    showMessage("系统错误 ，请稍后在试");
                }

                break;
        }


    }

    @Override
    public void showAccount(Account info) {
        Account account = mPresenter.getAccount();

        if (account!=null){
            tv_user_name.setText("用    户："+account.getName());
            expire_time.setText("到    期："+ TimeUtils.millis2String(Long.parseLong(account.getVip())*1000) );
        }
    }
}
